package com.ydj.framework.swagger;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.*;
import java.util.function.Predicate;


@Configuration
@EnableSwagger2
@EnableKnife4j
public class SwaggerConfig implements WebMvcConfigurer {

    private final OpenApiExtensionResolver openApiExtensionResolver;

    @Autowired
    public SwaggerConfig(OpenApiExtensionResolver openApiExtensionResolver) {
        this.openApiExtensionResolver = openApiExtensionResolver;
    }

    @Bean
    public Docket createCmsAdminApi() {
        String[] antPatternList = {"/cms/**"};
        return createDocket("后台管理端", antPatternList, "CMS");
    }

    @Bean
    public Docket createWebSidebarApi() {
        String[] antPatternList = {"/web/**"};
        return createDocket("网页端", antPatternList, "WEB");
    }

    @Bean
    public Docket createStreamSidebarApi() {
        String[] antPatternList = {"/stream/**"};
        return createDocket("网页端对话", antPatternList, "对话");
    }

    private Docket createDocket(String title, String[] antPatternList, String groupName) {
        Predicate<String> pathsPredicate = null;
        for (String antPattern : antPatternList) {
            if (pathsPredicate == null) {
                pathsPredicate = PathSelectors.ant(antPattern);
            } else {
                pathsPredicate = pathsPredicate.or(PathSelectors.ant(antPattern));
            }
        }
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        //页面标题
                        .title(title)
                        //条款地址
                        .termsOfServiceUrl("https://")
                        .contact(new Contact("Rick", "", ""))
                        .version("V1.0")
                        //描述
                        .description(title + "，前后端通讯协议")
                        .build())
                .groupName(groupName)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.ydj"))
                .paths(pathsPredicate)
                .build()
                .useDefaultResponseMessages(false)
                .extensions(openApiExtensionResolver.buildSettingExtensions());
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
//        registry.addResourceHandler("/swagger.json").addResourceLocations("classpath:/META-INF/resources/");
    }
}
